import axios from 'axios';
axios.defaults.timeout = 100000; // 请求超时时间
const baseUrl = 'https://a4a8oaqd9t-3.algolianet.com/1/indexes';

export default function (type = 'get', url: string, data: any, options = {}) {
    return new Promise((resolve, reject) => {
        type = type.toLowerCase();
        const config: any = {
            url: baseUrl + url,
            method: type,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Algolia-Api-Key': '5a234c15f4660fca4952722f023252a3',
                'X-Algolia-Application-Id': 'A4A8OAQD9T'
            },
            ...options

        };
        if (type === 'get') {
            config.params = data;
        } else if (type === 'post') {
            config.data = data;
        }

        // 添加响应拦截器
        // @ts-ignore
        axios.interceptors.response.use(function (response) { // 响应数据处理
            if (!response) return reject(response);
            return response;
        }, function (error) { // 响应错误处理
            return reject(error);
        });

        axios(config).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err);
        });
    });
}
